package com.example.seakdreamandroid.bedtimeStory;

import static com.example.seakdreamandroid.Constant.BASE_URL;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.seakdreamandroid.R;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Fragment1 extends Fragment{
    private GridView gridView;

    private List<Audio> Audiolist = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, null);
        gridView = view.findViewById(R.id.gv);
        setView();
        return view;
    }
    public void setView() {
        //2.创建请求对象（Request）,不需要设置请求体
        Request request = new Request.Builder()
                //设置请求的地址，默认的请求方式是Get请求
                .url(BASE_URL+"AudioInfo/findAllAudioInfo"+"?type=story")
                //创建Request对象
                .build();
        //3.发送请求，获取响应
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(request);
        //发起异步的请求，不需要手动创建子线程
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //请求失败时回调
                Log.e("OkHttp","onFailure");
                e.printStackTrace();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //请求成功时回调
                String reslut = response.body().string();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            ArrayList<Audio> Audios = new ArrayList<>();
                            JSONArray array = new JSONArray(reslut);
                            for (int n=0;n<array.length();n=n+2){
                            Audio audio1 = new Audio();
                            JSONObject object1 = (JSONObject) array.get(n);
                            audio1.setImage(object1.getString("audioPic"));
                            audio1.setText1(object1.getString("audioName"));
                            audio1.setText2(object1.getString("audioAuthor"));
                            audio1.setText3(object1.getString("type"));
                            audio1.setUrl(object1.getString("audioUrl"));
                            audio1.setId(object1.getString("audioId"));
                            audio1.setIf_vip(object1.getString("ifVip"));
                            Audios.add(audio1);
                        }
                        Audiolist.addAll(0,Audios);
                        }catch (Exception e){
                        e.printStackTrace();
                        }
                        GridViewMethod gridViewMethod = new GridViewMethod(
                                getContext(),R.layout.item,Audiolist
                        );
                        gridView.setAdapter(gridViewMethod);
                    }
                });
            }
        });
    }
}
